package com.xiaoshuidi.cloud.module.bpm.job.delayjob.strategy;

import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.module.bpm.controller.admin.swo.vo.TicketDelayJobParamDTO;
import com.xiaoshuidi.cloud.module.bpm.dal.dataobject.swo.SwoTicketDO;
import com.xiaoshuidi.cloud.module.bpm.dal.mysql.swo.SwoTicketMapper;
import com.xiaoshuidi.cloud.module.bpm.enums.swo.TicketServiceStatusEnum;
import com.xiaoshuidi.cloud.module.bpm.job.delayjob.TicketDelayJobService;
import com.xiaoshuidi.cloud.module.bpm.job.delayjob.TicketDelayJobTypeEnum;
import com.xiaoshuidi.cloud.module.bpm.service.message.BpmMessageService;
import com.xiaoshuidi.cloud.module.bpm.service.message.dto.MessageWhenSwoTicketRemindReqDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

/**
 * 维修工单-完工超时提醒
 */
@Slf4j
@Component
public class CompletionTimeoutReminderStrategy implements IStrategy {

    @Resource
    private SwoTicketMapper swoTicketMapper;
    @Resource
    private BpmMessageService bpmMessageService;
    @Resource
    private TicketDelayJobService ticketDelayJobService;
    // 完工超时-间隔时间
    @Value("${bpm.completion_timeout_interval_time}")
    private Long completionTimeoutIntervalTime;

    @Override
    public void consumerTask(Object taskParam) {
        log.info("[服务工单 - 完工超时提醒延迟任务执行], 当前时间: {}, 任务参数: {}", LocalDateTime.now(), JsonUtils.toJsonString(taskParam));
        TicketDelayJobParamDTO ticketDelayJobParamDTO = (TicketDelayJobParamDTO) taskParam;
        SwoTicketDO swoTicketDO = swoTicketMapper.selectById(ticketDelayJobParamDTO.getSwoTicketId());
        log.info("[服务工单 - 完工超时提醒延迟任务执行], 工单详情: {}", JsonUtils.toJsonString(swoTicketDO));
        // 工单已完成, 不需要提醒
        if(TicketServiceStatusEnum.COMPLETED.getStatus().equals(swoTicketDO.getStatus())){
            return;
        }
        // 当前跟进人不是发起延迟任务时的跟进人，不需要提醒
        if(!ticketDelayJobParamDTO.getAssigneeId().equals(swoTicketDO.getAssignee())){
            return;
        }
        // 发送短信提醒
        String msg = Integer.valueOf(1).equals(ticketDelayJobParamDTO.getReminderType()) ? "即将完工超时，" : "已经完工超时，";
        // 时间变量， 第一次执行时，加两个小时， 令短信在完工时间之后固定的时间检查执行
        Long timeVariable = Integer.valueOf(1).equals(ticketDelayJobParamDTO.getReminderType()) ? 2L : 0L;
        MessageWhenSwoTicketRemindReqDTO swoTicketRemindReqDTO = new MessageWhenSwoTicketRemindReqDTO()
                .setUserId(swoTicketDO.getAssignee())
                .setParam(msg)
                .setRoomFullName(swoTicketDO.getRoomFullName());
        bpmMessageService.sendMessageWhenSwoTicketRemind(swoTicketRemindReqDTO);
        // 同时设置24小时后再次执行一次任务， 检查是否完成
        TicketDelayJobParamDTO delayJobParamDto = new TicketDelayJobParamDTO();
        delayJobParamDto.setSwoTicketId(swoTicketDO.getId());
        delayJobParamDto.setAssigneeId(swoTicketDO.getAssignee());
        delayJobParamDto.setReminderType(2);
        ticketDelayJobService.addDelayJob(TicketDelayJobTypeEnum.COMPLETION_TIMEOUT_REMINDER, delayJobParamDto, completionTimeoutIntervalTime + timeVariable, TimeUnit.HOURS);
        log.info("[服务工单 - 完工超时提醒延迟任务执行], 工单ID: {}, 已发送短信提醒，已添加延迟任务，{}小时后再次执行检查", swoTicketDO.getId(), completionTimeoutIntervalTime);
    }
}
